/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.examenes.vista;

import basedatos.ExamenImagenBD;
import basedatos.OperacionesBD;
import java.io.File;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import modulos.consulta.modelo.Consulta;
import modulos.examenes.modelo.ExamenImagen;
import vista.frmPrincipal;
import vista.modelo.CopiaArchivos;
import vista.modelo.OperacionesVarias;
import vista.modelo.PonerImagenLabel;

/**
 *
 * @author Edulima
 */
public class iFrmExamenesImagen extends javax.swing.JInternalFrame {

    /**
     * Creates new form iFrmExamenesImagen
     */
    private ImageIcon icono;
    private static Consulta consulta = new Consulta();
    private static ArrayList<Object> listaExamenes = new ArrayList<Object>();
    private static DefaultListModel modelolistExamenes = new DefaultListModel();
    private ExamenImagen examenImagen = new ExamenImagen();

    public iFrmExamenesImagen(Consulta consulta) {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        this.consulta = consulta;
        listarExamenes();
        rbtnImagen1.setVisible(false);
        rbtnImagen2.setVisible(false);
        rbtnImagen3.setVisible(false);
        btnVisualizar.setEnabled(false);
    }

    public static void listarExamenes() {
        modelolistExamenes.removeAllElements();
        listaExamenes = (ArrayList<Object>) ExamenImagenBD.listarExamenes(consulta);
        for (Object obj : listaExamenes) {
            modelolistExamenes.addElement(((ExamenImagen) obj));
        }
         limpiar();
    }

    private void vistaPreviaImagenes(ExamenImagen examenImagen) {
        ArrayList<String> archivos = new ArrayList<String>();
        archivos.add(examenImagen.getImagen1());
        archivos.add(examenImagen.getImagen2());
        archivos.add(examenImagen.getImagen3());
        int cont = 0;
        rbtnImagen1.setVisible(false);
        rbtnImagen2.setVisible(false);
        rbtnImagen3.setVisible(false);
        for (String path : archivos) {
            cont++;
            File f = new File(path);
            switch (cont) {
                case 1:
                    icono = new ImageIcon(path);
                    lblImagen1.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
                    rbtnImagen1.setText(f.getName());
                    break;
                case 2:
                    icono = new ImageIcon(path);
                    lblImagen2.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
                    rbtnImagen2.setText(f.getName());
                    break;
                case 3:
                    icono = new ImageIcon(path);
                    lblImagen3.setIcon(PonerImagenLabel.loadImage(icono.getImage()));
                    rbtnImagen3.setText(f.getName());
                    break;
            }
            if (!path.isEmpty()) {
                switch (cont) {
                    case 1:
                        rbtnImagen1.setVisible(true);
                        break;
                    case 2:
                        rbtnImagen2.setVisible(true);
                        break;
                    case 3:
                        rbtnImagen3.setVisible(true);
                        break;

                }
            }
        }
        txtComentario.setText(examenImagen.getComentario());
        rbtnImagen1.setSelected(true);
    }

    private static void limpiar(){
        txtComentario.setText("");
        lblImagen1.setIcon(null);
        lblImagen2.setIcon(null);
        lblImagen3.setIcon(null);
        rbtnImagen1.setText("");
        rbtnImagen2.setText("");
        rbtnImagen3.setText("");
        rbtnImagen1.setVisible(false);
        rbtnImagen2.setVisible(false);
        rbtnImagen3.setVisible(false);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnVisualizar = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listExamenes = new javax.swing.JList();
        jPanel4 = new javax.swing.JPanel();
        pnlImagen1 = new javax.swing.JPanel();
        lblImagen1 = new javax.swing.JLabel();
        rbtnImagen1 = new javax.swing.JRadioButton();
        pnlImagen2 = new javax.swing.JPanel();
        lblImagen2 = new javax.swing.JLabel();
        rbtnImagen2 = new javax.swing.JRadioButton();
        pnlImagen3 = new javax.swing.JPanel();
        lblImagen3 = new javax.swing.JLabel();
        rbtnImagen3 = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtComentario = new javax.swing.JTextArea();
        btnGuardar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setClosable(true);
        setTitle("EXAMENES DE IMAGEN");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/imagen.png"))); // NOI18N
        getContentPane().setLayout(null);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(530, 470, 103, 27);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/add.png"))); // NOI18N
        jButton2.setText("Nuevo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(530, 20, 110, 27);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/delete.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEliminar);
        btnEliminar.setBounds(530, 80, 110, 27);

        btnVisualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/Visualizar.png"))); // NOI18N
        btnVisualizar.setText("Visualizar");
        btnVisualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarActionPerformed(evt);
            }
        });
        getContentPane().add(btnVisualizar);
        btnVisualizar.setBounds(200, 470, 122, 27);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/icono_editar.png"))); // NOI18N
        jButton5.setText("Editar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5);
        jButton5.setBounds(530, 50, 110, 27);

        listExamenes.setBorder(javax.swing.BorderFactory.createTitledBorder("Examenes"));
        listExamenes.setModel(modelolistExamenes);
        listExamenes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listExamenesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listExamenes);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(12, 12, 503, 141);

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        lblImagen1.setBorder(new javax.swing.border.MatteBorder(null));

        buttonGroup1.add(rbtnImagen1);
        rbtnImagen1.setSelected(true);
        rbtnImagen1.setText("Imagen 1");

        javax.swing.GroupLayout pnlImagen1Layout = new javax.swing.GroupLayout(pnlImagen1);
        pnlImagen1.setLayout(pnlImagen1Layout);
        pnlImagen1Layout.setHorizontalGroup(
            pnlImagen1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlImagen1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlImagen1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(rbtnImagen1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblImagen1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlImagen1Layout.setVerticalGroup(
            pnlImagen1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlImagen1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImagen1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnImagen1)
                .addContainerGap())
        );

        lblImagen2.setBorder(new javax.swing.border.MatteBorder(null));

        buttonGroup1.add(rbtnImagen2);
        rbtnImagen2.setText("Imagen 2");

        javax.swing.GroupLayout pnlImagen2Layout = new javax.swing.GroupLayout(pnlImagen2);
        pnlImagen2.setLayout(pnlImagen2Layout);
        pnlImagen2Layout.setHorizontalGroup(
            pnlImagen2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlImagen2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlImagen2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtnImagen2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblImagen2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlImagen2Layout.setVerticalGroup(
            pnlImagen2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlImagen2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImagen2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnImagen2)
                .addContainerGap())
        );

        lblImagen3.setBorder(new javax.swing.border.MatteBorder(null));

        buttonGroup1.add(rbtnImagen3);
        rbtnImagen3.setText("Imagen 3");
        rbtnImagen3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnImagen3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlImagen3Layout = new javax.swing.GroupLayout(pnlImagen3);
        pnlImagen3.setLayout(pnlImagen3Layout);
        pnlImagen3Layout.setHorizontalGroup(
            pnlImagen3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlImagen3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlImagen3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(rbtnImagen3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblImagen3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlImagen3Layout.setVerticalGroup(
            pnlImagen3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlImagen3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblImagen3, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnImagen3)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(pnlImagen1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(pnlImagen2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(44, 44, 44)
                .addComponent(pnlImagen3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlImagen1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlImagen2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pnlImagen3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        getContentPane().add(jPanel4);
        jPanel4.setBounds(12, 294, 503, 168);

        txtComentario.setColumns(20);
        txtComentario.setLineWrap(true);
        txtComentario.setRows(5);
        jScrollPane2.setViewportView(txtComentario);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(12, 182, 503, 100);

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/filesave.png"))); // NOI18N
        btnGuardar.setText("Guardar ");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });
        getContentPane().add(btnGuardar);
        btnGuardar.setBounds(530, 220, 110, 27);

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel2.setText("Comentario sobre examen");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(180, 160, 210, 17);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-667)/2, (screenSize.height-536)/2, 667, 536);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        iFrmExamenesImagen1 examenes = new iFrmExamenesImagen1(consulta);
        frmPrincipal.dskpPrincipal.add(examenes);
        OperacionesVarias.centrar(examenes);

    }//GEN-LAST:event_jButton2ActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        int index = listExamenes.getSelectedIndex();
        if (index != -1) {
            int opc = JOptionPane.showConfirmDialog(this, "Esta seguro de eliminar el examen del paciente \n" + examenImagen.getConsulta().getPaciente(), "Eliminar Examen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (opc == 0) {
                CopiaArchivos c=new CopiaArchivos();
                File delete= new File(c.directorioBase()+ "/" + consulta.getUsuario().getNombre() + " "
                        + consulta.getUsuario().getApellido() + " " + consulta.getUsuario().getEspecialidad() + "/"
                        + consulta.getPaciente().getNumeroHC() + "/" + consulta.getNumConsul() + "/" + examenImagen.getNombreExamen());
                System.out.println(delete.getAbsoluteFile());
                CopiaArchivos.BorrarDirLocal(delete);
                OperacionesBD.eliminar(examenImagen);
                listarExamenes();            
            }
        } else {
            JOptionPane.showMessageDialog(this, "Seleccione el examen que desea eliminar", "Informacion", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        iFrmExamenesImagen1 examenes = new iFrmExamenesImagen1(examenImagen);
        frmPrincipal.dskpPrincipal.add(examenes);
        OperacionesVarias.centrar(examenes);
    }//GEN-LAST:event_jButton5ActionPerformed

    private void listExamenesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listExamenesValueChanged
        int opc = listExamenes.getSelectedIndex();
        if (opc != -1) {
            examenImagen = (ExamenImagen) listExamenes.getSelectedValue();
            vistaPreviaImagenes(examenImagen);
            btnVisualizar.setEnabled(true);
        }
    }//GEN-LAST:event_listExamenesValueChanged

    private void rbtnImagen3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnImagen3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rbtnImagen3ActionPerformed

    private void btnVisualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarActionPerformed
        String path = "";
        if (rbtnImagen1.isSelected()) {
            path = examenImagen.getImagen1();
        } else if (rbtnImagen2.isSelected()) {
            path = examenImagen.getImagen2();
        } else if (rbtnImagen3.isSelected()) {
            path = examenImagen.getImagen3();
        }
        iFrmVisualizarImagen vp = new iFrmVisualizarImagen(path);
        frmPrincipal.dskpPrincipal.add(vp);
        OperacionesVarias.centrar(vp);

    }//GEN-LAST:event_btnVisualizarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        examenImagen.setComentario(txtComentario.getText());
        OperacionesBD.modificar(examenImagen);
    }//GEN-LAST:event_btnGuardarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnVisualizar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private static javax.swing.JLabel lblImagen1;
    private static javax.swing.JLabel lblImagen2;
    private static javax.swing.JLabel lblImagen3;
    private javax.swing.JList listExamenes;
    private javax.swing.JPanel pnlImagen1;
    private javax.swing.JPanel pnlImagen2;
    private javax.swing.JPanel pnlImagen3;
    private static javax.swing.JRadioButton rbtnImagen1;
    private static javax.swing.JRadioButton rbtnImagen2;
    private static javax.swing.JRadioButton rbtnImagen3;
    private static javax.swing.JTextArea txtComentario;
    // End of variables declaration//GEN-END:variables
}
